package com.citrinus.feedme.location;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;

import com.citrinus.feedme.R;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.maps.LocationSource;
import com.google.android.gms.maps.model.LatLng;

/**
 * TODO define location update parameters after 10 min or after 1/2 mile run
 * request for update
 * 
 * @author pedrolaranjeiro
 * 
 */
public class LocationHandler implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, LocationListener,
		LocationSource {

	private static final String TAG = LocationHandler.class.getSimpleName();

	private LocationClient locationClient;
	private LocationChangeListener locationChangeListener;
	private long updateInterval = 5 * 60 * 1000; // update every 5 minutes

	public LocationHandler(Context context) {
		locationClient = new LocationClient(context, this, this);
	}

	public void requestLocationUpdates() {
		if (!locationClient.isConnected())
			return;

		LocationRequest locationRequest = LocationRequest.create();
		locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
		locationRequest.setInterval(updateInterval);

		locationClient.requestLocationUpdates(locationRequest, this);
	}

	public boolean startLocationServices() {
		if (locationClient.isConnected())
			return false;

		locationClient.connect();
		return true;
	}

	public void stopLocationServices() {
		if (!locationClient.isConnected())
			return;

		if (locationClient.isConnected()) {
			locationClient.removeLocationUpdates(this);
		}

		locationClient.disconnect();
	}

	@Override
	public void activate(OnLocationChangedListener arg0) {
		// TODO Auto-generated method stub
		Log.d(TAG, "activate");

	}

	@Override
	public void deactivate() {
		// TODO Auto-generated method stub
		Log.d(TAG, "deactivate");

	}

	@Override
	public void onLocationChanged(Location location) {
		LatLng position = new LatLng(location.getLatitude(), location.getLongitude());
		locationChangeListener.onLocationUpdateListener(position);
	}

	@Override
	public void onConnectionFailed(ConnectionResult arg0) {
		// TODO Auto-generated method stub
		Log.d(TAG, "on connection failed");

	}

	@Override
	public void onConnected(Bundle arg0) {
		requestLocationUpdates();
		Log.d(TAG, "is connected");
	}

	@Override
	public void onDisconnected() {
		// TODO Auto-generated method stub
		Log.d(TAG, "is disconnected");

	}

	public void setListener(LocationChangeListener locationChangeListener) {
		this.locationChangeListener = locationChangeListener;
	}

	public boolean isLocationEnable(final Context context) {
		LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
		boolean isGpsEnable = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
		boolean isNetworkAvailable = isNetworkAvailable(context);

		if (!isGpsEnable && !isNetworkAvailable) {
			AlertDialog.Builder builder = new AlertDialog.Builder(context);
			builder.setTitle(R.string.dialog_location_title); // GPS not found
			builder.setMessage(R.string.dialog_location_msg); // Want to
															// enable?
			builder.setPositiveButton(R.string.dialog_btn_set, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialogInterface, int i) {
					context.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
				}
			});
			builder.setNegativeButton(R.string.dialog_btn_net, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialogInterface, int i) {
					context.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
				}
			});

			builder.create().show();
			return false;
		}
		return true;
	}

	public boolean isNetworkAvailable(Context context) {
		ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
		return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting();
	}
	
	public void showNoInternetDialog(final Context context){
		
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(R.string.dialog_wireless_title); // GPS not found
		builder.setMessage(R.string.dialog_wireless_msg); // Want to
														// enable?
		builder.setPositiveButton(R.string.dialog_btn_set, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialogInterface, int i) {
				context.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
			}
		});

		builder.create().show();
		
	}

}
